package cn.zhentao.controller;

import cn.zhentao.dtos.ResponseResult;
import cn.zhentao.manger.dto.LoginMangerDto;
import cn.zhentao.manger.dto.User_list_Param;
import cn.zhentao.service.SysUserService;
import cn.zhentao.service.TUserService;
import cn.zhentao.user.pojo.TUser;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员登录
 */
@RestController
@RequestMapping("manger")
public class SysUserContro {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private TUserService tUserService;
    //管理员登录
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginMangerDto dto){
     return sysUserService.login(dto);
    }

    /*管理员个人信息*/
    @GetMapping("marginInfo")
    public ResponseResult marginInfo(@RequestParam("id") Integer id){
        return sysUserService.marginInfo(id);
    }
    /*查询用户信息*/
    @PostMapping("/manger_list")
    public ResponseResult manger_list(@RequestBody User_list_Param param){
        Page<TUser> userPage = sysUserService.userpage(param);
        return ResponseResult.okResult(userPage);
    }
    //禁用
    @GetMapping("user_enable")
    public ResponseResult user_enable(@RequestParam("id") Integer id){
        if(id == null){
            return ResponseResult.errorResult(500,"用户id不能为空");
        }
        TUser user = tUserService.getById(id);
        if(user.getStatus() == 1){ //如果等于1返回给客户端
            return ResponseResult.errorResult(500, "您已设为禁用状态!!!");
        }
            user.setStatus(1);//启用
            tUserService.updateById(user);
        return ResponseResult.okResult("设置禁用状态成功");
    }
   //启用
    @GetMapping("user_blan")
    public ResponseResult user_blan(@RequestParam("id") Integer id){
        if(id == null){
            return ResponseResult.errorResult(500,"用户id不能为空");
        }
        TUser user = tUserService.getById(id);
        if(user.getStatus() == 0){ //如果等于0说明为启用
            return ResponseResult.errorResult(500,"您已设为启用状态");
        }
            user.setStatus(0);//启用
            tUserService.updateById(user);
        return ResponseResult.okResult("设置启用状态成功");
    }
    //查询用户所有
    @GetMapping("userlist")
    public ResponseResult userlist(){
        List<TUser> list = tUserService.list();
       List<Integer> listid = new ArrayList<>();

        for (TUser user : list) {
          listid.add(user.getId());
        }

        return ResponseResult.okResult(listid);
    }
}
